# -*- coding: iso-8859-1 -*-
#
#    This is part of Malvinas Argentinas Messenger
#
#    Malvinas Argentinas Messenger is free software; you can redistribute
#    it and/or modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2 of the
#    License, or (at your option) any later version.
#
#    Malvinas Argentinas Messenger is distributed in the hope that it will be
#    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Malvinas Argentinas Messenger; if not, write to the Free
#    Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#    MA  02110-1301  USA

import curses
import pad

INPUT_TIMEOUT = 10
INPUT_HISTORY_LINES = 30

class InputPad(pad.Pad):
	def __init__(self, rows, cols, y, x):
		pad.Pad.__init__(self, rows, cols, y, x)
		self.p.timeout(INPUT_TIMEOUT)
		self.p.keypad(1)
		self.attr = None
		self.hidden = 0
		self.buff = ''
		self.pos = 0
		self.history_lines = INPUT_HISTORY_LINES
		self.history_index = 0
		self.history = []
		
	def setAttr(self, attr):
		self.attr = attr
		
	def resize(self, sizeY, sizeX, y, x):
		pad.Pad.resize(self, sizeY, sizeX, y, x)
		self.p.timeout(INPUT_TIMEOUT)
		self.p.keypad(1)
		
	def delChar(self):
		if self.pos >= 1:
			self.pos -= 1
			self.buff = self.buff[:self.pos] + self.buff[self.pos+1:]
			if not self.hidden:
				self.p.erase()
				if self.attr:
					self.p.addstr(self.buff, self.attr)
				else:
					self.p.addstr(self.buff)
				self.cursPos(self.pos)
				
	def cursPos(self, pos):
		try:
			y = pos / self.cols
			x = pos % self.cols
			self.p.move(y, x)
			self.w.move(y, x)
		except:
			pass
			
	# For pads in options win
	def refresh_inOptWin(self):
		y = self.pos / self.cols
		x = self.pos % self.cols
		try:
			self.p.refresh(y, 0, self.y, self.x, self.y, self.x + self.cols - 1)
		except curses.error:
			pass
			
	def cursBegin(self):
		self.pos = 0
		self.cursPos(self.pos)
		
	def cursEnd(self):
		self.pos = len(self.buff)
		self.cursPos(self.pos)
		
	def cursBack(self):
		if self.pos >= 1:
			self.pos -= 1
			self.cursPos(self.pos)
			
	def cursForward(self):
		if self.pos < len(self.buff):
			self.pos += 1
			self.cursPos(self.pos)
			
	def clear(self):
		self.buff = ''
		self.pos = 0
		self.cursPos(self.pos + 1)
		self.text = []
		self.p.erase()
		self.refresh()
		
	def addstr (self, text, attr = None):
		if self.pos == len(self.buff):
			self.buff += text
			self.pos += len(text)
			if attr:
				self.p.addstr(text, attr)
			else:
				self.p.addstr(text)
		else:
			self.buff = self.buff[:self.pos] + text + self.buff [self.pos:]
			self.p.erase()
			if attr:
				self.p.addstr(self.buff, attr)
			else:
				self.p.addstr(self.buff)
			self.refresh()
			self.pos += 1
			self.cursPos(self.pos)		
